// Template system types and interfaces

export interface TemplateSection {
  id: string;
  title: string;
  prompt?: string;
  placeholder: string;
  required: boolean;
  order: number;
}

export interface DiaryTemplate {
  id: string;
  name: string;
  description: string;
  icon?: string;
  category: TemplateCategory;
  sections: TemplateSection[];
  defaultTags?: string[];
  tags?: string[];
  isCustom: boolean;
  createdBy?: string;
  usageCount: number;
  isFavorite: boolean;
  color?: string; // Theme color for the template
  estimatedTime?: number;
  createdAt?: Date;
  updatedAt?: Date;
}

export interface TemplateCategoryDetail {
  id: string;
  name: string;
  description: string;
  icon: string;
  color: string;
}

// Extended DiaryEntry interface to support templates
export interface TemplateEntry {
  templateId?: string;
  templateSections?: { [sectionId: string]: string };
}

// Template usage analytics
export interface TemplateUsage {
  templateId: string;
  usedAt: string;
  completedSections: number;
  totalSections: number;
}

// Template store state
export interface TemplateStore {
  templates: DiaryTemplate[];
  categories: TemplateCategory[];
  favoriteTemplates: string[];
  recentTemplates: string[];
  templateUsage: TemplateUsage[];
  
  // Actions
  addTemplate: (template: Omit<DiaryTemplate, 'id' | 'usageCount'>) => void;
  updateTemplate: (id: string, updates: Partial<DiaryTemplate>) => void;
  deleteTemplate: (id: string) => void;
  toggleFavorite: (templateId: string) => void;
  recordTemplateUsage: (templateId: string, completedSections: number, totalSections: number) => void;
  
  // Getters
  getTemplateById: (id: string) => DiaryTemplate | undefined;
  getTemplatesByCategory: (category: string) => DiaryTemplate[];
  getFavoriteTemplates: () => DiaryTemplate[];
  getRecentTemplates: (limit?: number) => DiaryTemplate[];
  getPopularTemplates: (limit?: number) => DiaryTemplate[];
}

// Predefined template categories
export const TEMPLATE_CATEGORIES = {
  personal: 'Personal',
  wellness: 'Wellness', 
  productivity: 'Productivity',
  creative: 'Creative',
  travel: 'Travel',
  relationships: 'Relationships'
} as const;

export type TemplateCategory = keyof typeof TEMPLATE_CATEGORIES;

export const TEMPLATE_CATEGORY_DETAILS: TemplateCategoryDetail[] = [
  {
    id: 'personal',
    name: 'Personal',
    description: 'Daily reflection and personal growth',
    icon: 'User',
    color: '#3B82F6'
  },
  {
    id: 'wellness',
    name: 'Wellness',
    description: 'Health, mood, and self-care tracking',
    icon: 'Heart',
    color: '#EF4444'
  },
  {
    id: 'productivity',
    name: 'Productivity',
    description: 'Work, goals, and achievement tracking',
    icon: 'Target',
    color: '#10B981'
  },
  {
    id: 'creative',
    name: 'Creative',
    description: 'Artistic expression and inspiration',
    icon: 'Palette',
    color: '#8B5CF6'
  },
  {
    id: 'travel',
    name: 'Travel',
    description: 'Adventures and travel experiences',
    icon: 'MapPin',
    color: '#F59E0B'
  },
  {
    id: 'relationships',
    name: 'Relationships',
    description: 'Social connections and interactions',
    icon: 'Users',
    color: '#EC4899'
  }
];

// Template validation
export interface TemplateValidation {
  isValid: boolean;
  errors: string[];
}

export const validateTemplate = (template: Partial<DiaryTemplate>): TemplateValidation => {
  const errors: string[] = [];
  
  if (!template.name || template.name.trim().length === 0) {
    errors.push('Template name is required');
  }
  
  if (!template.description || template.description.trim().length === 0) {
    errors.push('Template description is required');
  }
  
  if (!template.sections || template.sections.length === 0) {
    errors.push('Template must have at least one section');
  }
  
  if (template.sections) {
    template.sections.forEach((section, index) => {
      if (!section.title || section.title.trim().length === 0) {
        errors.push(`Section ${index + 1} must have a title`);
      }
      if (!section.placeholder || section.placeholder.trim().length === 0) {
        errors.push(`Section ${index + 1} must have a placeholder`);
      }
    });
  }
  
  return {
    isValid: errors.length === 0,
    errors
  };
};